﻿using Streetlight.Store.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Streetlight.Store.MvcApplication.Controllers
{
    public class ProductsController : StoreController
    {
        public ActionResult Index()
        {
            List<Product> model = TransactionManager.Products.ListAll();

            return View("List", model);
        }

        public ActionResult List()
        {
            List<Product> model = TransactionManager.Products.ListAll();

            return View(model);
        }

        public ActionResult Create()
        {
            Product model = new Product();

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(Product model)
        {
            if (TransactionManager.Products.DuplicateItemNumberExists(string.Empty, model.ItemNumber))
            {
                ModelState.AddModelError("ItemNumber", "A product already exists with this item number.");
            }

            if (TransactionManager.Products.DuplicateGlobalIdManufacturerExists(string.Empty, model.GlobalId, model.Manufacturer))
            {
                ModelState.AddModelError("GlobalId", "A product already exists with this Global ID/Manufacturer combination.");
            }

            if (ModelState.IsValid)
            {
                TransactionManager.Products.CreateNew(model);

                return RedirectToAction("Edit", new { id = model.Id });
            }
            else
            {
                return View(model);
            }
        }

        public ActionResult Edit(string id)
        {
            Product model = TransactionManager.Products.GetProduct(id);

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(Product model)
        {
            if (TransactionManager.Products.DuplicateItemNumberExists(model.Id, model.ItemNumber))
            {
                ModelState.AddModelError("ItemNumber", "A product already exists with this item number.");
            }

            if (TransactionManager.Products.DuplicateGlobalIdManufacturerExists(model.Id, model.GlobalId, model.Manufacturer))
            {
                ModelState.AddModelError("GlobalId", "A product already exists with this Global ID/Manufacturer combination.");
            }

            if (ModelState.IsValid)
            {
                TransactionManager.Products.Save(model);
            }

            return View(model);
        }

        public ActionResult CategoryList(string id)
        {
            ViewBag.Id = id;

            List<ProductCategory> model = TransactionManager.Products.ListCategoriesForProduct(id);

            return PartialView(model);
        }

        public ActionResult AddCategory(string id)
        {
            ViewBag.Id = id;

            List<ProductCategory> model = TransactionManager.Products.ListAllCategories();

            return PartialView(model);
        }

        [HttpPost]
        public ActionResult AddCategory(string id, string categoryId)
        {
            TransactionManager.Products.AddProductToCategory(id, categoryId);

            return RedirectToAction("Edit", new { id = id });
        }

        public ActionResult RemoveCategory(string id, string categoryId)
        {
            TransactionManager.Products.RemoveProductFromCategory(id, categoryId);

            return RedirectToAction("Edit", new { id = id });
        }
    }
}
